<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2016-10-22
 * Time: 10:27
 */

namespace app\src\accountScore\action;


use app\src\accountScore\logic\AccountScoreHistoryLogic;
use app\src\accountScore\logic\AccountScoreLogic;
use app\src\base\action\BaseAction;

use app\src\seller\logic\SellerLogic;
use think\Db;

class AccountScoreAction extends BaseAction{
    const SCORE_MEMBER = 0;
    const SCORE_PLATFORM = 1;
    const SCORE_SELLER = 2;
    /*
     * 处理消费产生的收益
     */
    public function dealBuy($date){
        if(empty($date)) return $this->error('没有数据');
        $array=['0','1','1_5','2','3','4','5','6'];
        $type = $date['type'];
        foreach($array as $k=>$v){
            $key = 'p'.$v;
            if(!empty($date[$key])){
                $group = $this->getGroup($v,$type);
                $s = 's'.$v;
                if($date[$s]>=1){
                    $result = (new AccountScoreLogic())->plusScore($group['group'],$date[$key],AccountScoreLogic::SCORE_TYPE_1,$date[$s],$group['reason']);
                }
                //if(!$result['status']) return $this->error($result['info']);
            }
        }
        return $this->success('处理收益完毕');
    }

    public function getGroup($key,$type){
        //判断是否是自己 如果是自己
        $array_member = ['0','1','1_5','3'];
        $array_seller = ['2'];
        $array_platform = ['4','5','6'];
        $reason='推荐会员联盟商家消费，获得收入';
        if($type == 1) $reason='推荐会员购买车险成功，获得收入';
        if($key == 0 && $type == 2) $reason='到联盟商家消费，获得收入';
        $group = 0;
        if(in_array($key,$array_member)){
            $group = 0;
        }elseif(in_array($key,$array_seller)){
            $group = 2;
        }elseif(in_array($key,$array_platform)){
            $group = 1;
        }
        return['group'=>$group,'reason'=>$reason];
    }

    /*
     *查询用户的佣金
     */
    public function queryAccounts($type,$uid,$score_type,$page,$change_type){
        $map['type'] = $type;
        $map['account_id'] = $uid;
        $map['score_type'] = $score_type;
        $order = 'create_time desc';
        $map['change_type'] = $change_type;
        $result = (new AccountScoreHistoryLogic())->queryWithPagingHtml($map,$page,$order);
        $count =  (new AccountScoreHistoryLogic())->count($map);
        if($result['status']){
            return $this->success(['count'=>$count['info'],'list'=>$result['info']['list']]);
        }else{
            return $this->error($result['info']);
        }
    }



}